package com.example.depressionapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.VideoView;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


public class PsyTestActivity extends AppCompatActivity {

    private final int TEXT_QUESTION = 1;  //单选问卷
    private final int PICPLAY_QUESTION = 2;  //视频播放类问卷
    private final int RECORD = 3;     //录音问卷
    private final int VEDIOPLAY_QUESTION = 4; // 图片播放类问卷

    private int videoSequence = 0;
    private int pictureSequence = 0;
    private int balance = 0;
    int[] images = { R.drawable.negative1, R.drawable.negative2, R.drawable.negative3, };

    private MyButtonClick myButtonClick = new MyButtonClick();


    private Integer indexQue = 1;
    private Button btn_next;
    private TextView view_ques; //问卷信息框
    private LinearLayout que_normal;
    private LinearLayout que_record;
    private LinearLayout que_videoplay;
    private LinearLayout que_pictureplay;
    private String[] quesInfo ;

    private String answer;
    private List<String> answerList = new ArrayList<String>();


    private RadioGroup radioGroup;
    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private RadioButton radioButton3;
    private RadioButton radioButton4;

    private Button recordStart;
    private Button recordStop;

    private VideoView videoView;
    private ImageView imageView;


    public ArrayList<Integer> QueType = new ArrayList<Integer>(){{
        add(1);
        add(2);
        add(1);
        add(3);
        add(1);
        add(1);
        add(4);
        add(1);
        add(3);
        add(1);
        add(2);
        add(1);
        add(3);
        add(1);
        add(2);
        add(1);
        add(3);
        add(-1);
    }};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_psy_test);
        quesInfo = this.getResources().getStringArray(R.array.quesInfo);



        initView();

    }

    private void initView(){

        btn_next = findViewById(R.id.btn_next);
        view_ques = findViewById(R.id.view_ques);

        que_normal = findViewById(R.id.que_normal);
        que_record = findViewById(R.id.que_record);
        que_videoplay = findViewById(R.id.que_videoplay);
        que_pictureplay = findViewById(R.id.que_pictureplay);

        radioGroup = findViewById(R.id.radiogroup1);
        radioButton1 = findViewById(R.id.radiobutton1);
        radioButton2 = findViewById(R.id.radiobutton2);
        radioButton3 = findViewById(R.id.radiobutton3);
        radioButton4 = findViewById(R.id.radiobutton4);
        recordStart = findViewById(R.id.btn_start);
        recordStop = findViewById(R.id.btn_stop);

        videoView = findViewById(R.id.video);
        imageView = findViewById(R.id.picture);

        btn_next.setOnClickListener(myButtonClick);
        recordStart.setOnClickListener(myButtonClick);
        recordStop.setOnClickListener(myButtonClick);

        view_ques.setText(quesInfo[0]);

        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                videoSequence++;
                videoView.stopPlayback();
                if (videoSequence >= 3) {
                    videoView.setVideoURI(null);
                    videoSequence = 0;
                } else {
                    brcastVideo();
                }
            }
        });

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.radiobutton1:
                        answer = radioButton1.getText().toString();
                        break;
                    case R.id.radiobutton2:
                        answer = radioButton2.getText().toString();
                        break;
                    case R.id.radiobutton3:
                        answer = radioButton3.getText().toString();
                        break;
                    case R.id.radiobutton4:
                        answer = radioButton4.getText().toString();
                        break;
                    default:
                        break;
                }

                answerList.add(answer);
                answer = null;

            }
        });


    }



    private class MyButtonClick implements Button.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_next:
                    if (QueType.get(indexQue) != -1){
                        view_ques.setText(quesInfo[indexQue]);
                    }
                    switch (QueType.get(indexQue)){
                        case -1:
                            //问卷结束
                            Intent intent = new Intent();
                            intent.setClass(PsyTestActivity.this, MainActivity.class);
                            startActivity(intent);
                            break;
                        case TEXT_QUESTION:
                            //显示选项，隐藏播放控件和录音按钮

                            que_normal.setVisibility(View.VISIBLE);
                            que_record.setVisibility(View.GONE);
                            que_videoplay.setVisibility(View.GONE);
                            que_pictureplay.setVisibility(View.GONE);



                            //处理选项问卷
                            //需要相应类处理选项答案

                            break;
                        case VEDIOPLAY_QUESTION:
                            //显示播放控件，隐藏选项控件和录音按钮

                            que_videoplay.setVisibility(View.VISIBLE);
                            que_record.setVisibility(View.GONE);
                            que_normal.setVisibility(View.GONE);
                            que_pictureplay.setVisibility(View.GONE);



                            //处理播放问卷
                            //需要相关类进行播放视屏或图片
                            brcastVideo();

                            break;
                        case RECORD:
                            //显示录音按钮，隐藏选项控件和播放控件

                            que_record.setVisibility(View.VISIBLE);
                            que_normal.setVisibility(View.GONE);
                            que_videoplay.setVisibility(View.GONE);
                            que_pictureplay.setVisibility(View.GONE);


                            //处理录音问卷
                            //需要进行音频录制

                            break;
                        case PICPLAY_QUESTION:
                            //显示图片播放，隐藏选项控件和录音控件还有视频播放控件

                            que_pictureplay.setVisibility(View.VISIBLE);
                            que_record.setVisibility(View.GONE);
                            que_normal.setVisibility(View.GONE);
                            que_videoplay.setVisibility(View.GONE);

                            //处理图片播放
//                            brcastPicture();
                            PicturePlay picturePlay = new PicturePlay(imageView);
                            picturePlay.play();

                            pictureSequence = 0;
                            balance = 0;

                            break;

                        default:
                            Log.i("信息","未捕捉到按钮类型");
                            break;
                    }

                    indexQue++;
                    break;
                case R.id.btn_start:
                    //开始录音

                    break;
                case R.id.btn_stop:
                    //停止录音

                    break;
                default:
                    break;
            }
        }
    }

    private void brcastVideo(){

        switch(videoSequence) {
            case 0:
                videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/raw/negative"));
                break;
            case 1:
                videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/raw/neutral"));
                break;
            case 2:
                videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/raw/positive"));
                break;
        }
        videoView.start();
    }

//    private void brcastPicture(){
//
//        Timer timer = new Timer();//图片timer
//
//        Handler handler = new Handler() {
//            public void handleMessage(Message msg) {
//                super.handleMessage(msg);
//
//                if (pictureSequence >= images.length) {
//                    timer.cancel();
//                }else{
//                    if (msg.what == 1) {
//                        imageView.setImageResource(images[pictureSequence++]);
//                    }else if(msg.what == 0){
//                        imageView.setImageDrawable(null);
//                    }
//                }
//            }
//        };
//
//        timer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                Message msg = new Message();
//                if (balance == 0) {
//                    msg.what = 0;
//                    balance += 1;
//                } else {
//                    msg.what = 1;
//                    balance -= 1;
//                }
//                handler.sendMessage(msg);
//            }
//        }, 0, 3000);
//
//    }


}